Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

11.3 Memory Hotplug Management Initialization

The hotplug functionality of most memory hardware devices operates independently of the CPU; however, certain NUMA memory hardware devices mandate architectural CPU support to execute hotplug operations. Consequently, Linux must implement a NUMA memory hotplug management infrastructure to accommodate these NUMA memory components that cannot achieve standalone hotplug capabilities.

Linux divides the CPU hotplug management sequence into multiple explicit phases or states, which can be broadly classified into several operational stages: offline, silent, prepare, and online. Each stage is further segmented into diverse, granular steps, whose precise categorization can be reviewed inside the definition of the cpuhp_state enum class located within the file git/include/linux/cpuhotplug.h. Linux leverages the previously introduced cpuhp_cpu_state structure to record the runtime state of a CPU across different phases. This encompasses multiple structural fields tracking the CPU's current state, target state, currently running hotplug daemon thread, and whether it is undergoing a bringup or teardown transition.

Throughout hotplug management execution, a CPU residing at any given state possesses the potential to enter a low-power sleep phase, or be woken up and restored from distinct conditions. Similarly, a CPU might encounter exceptions or recover from faults at any state block. Therefore, each discrete state must be paired with corresponding startup and teardown routines. To preserve these platform-specific startup and teardown handlers defined in the form of callback functions under diverse states, Linux declares a global array named cpuhp_hp_states of type struct cpuhp_step. This array is defined within the source file git/kernel/cpu.c.

The definition of the cpuhp_step structure is as follows:

struct cpuhp_step {
	const char		*name;
	union {
		int		(*single)(unsigned int cpu);
		int		(*multi)(unsigned int cpu, struct hlist_node *node);
	} startup;
	union {
		int		(*single)(unsigned int cpu);
		int		(*multi)(unsigned int cpu, struct hlist_node *node);
	} teardown;
	struct hlist_head	list;
	bool			cant_stop;
	bool			multi_instance;
};

This structure is hosted within git/kernel/cpu.c, wherein the list field represents a hash linked list head used to preserve all active entities (instances) that invoke the matching startup() and teardown() callbacks of this specific tracking node.

Upon initializing the architectural zone structures of the available NUMA nodes, the start_kernel() process invokes page_alloc_init(). This routine triggers cpuhp_setup_state_nocalls() to register the teardown handler for the mm/page_alloc:dead step under the CPUHP_PAGE_ALLOC_DEAD hotplug state, assigning it to the function address of page_alloc_cpu_dead(). Structurally, this sets the name field of the cpuhp_step structure located at index slot cpuhp_hp_states[CPUHP_PAGE_ALLOC_DEAD] to "mm/page_alloc:dead", maps the teardown.single field to point to the entry address of the function page_alloc_cpu_dead(), sets the multi_instance flag to 0, and initializes the remaining tracking fields to NULL.

The function page_alloc_cpu_dead() acts as the memory reclamation interface within the core memory management subsystem. Its primary technical objective is to drain and release the cached memory pages archived inside the diverse local LRU (Least Recently Used) lists of the target dead CPU core. It drains these cached pages back into the fallback free_area tracking arrays of their respective architectural zone structures, and subsequently updates the corresponding __lruvec tracking field within the parent pglist_data node framework. The __lruvec field is of type struct lruvec and records the active runtime Least Recently Used (LRU) statistics of the physical memory block.

To facilitate high-efficiency memory reclamation passes, Linux partitions physical memory frames across distinct LRU tracking lists based on their exact active usage criteria (for instance, evaluating whether a page is backed by an active disk filesystem block or mapped onto an anonymous swap area). The struct lruvec structure acts as a composite wrapper grouping these diverse categories of LRU tracking links; interested readers can consult specialized Linux memory management documentation for comprehensive architectural analysis.

Throughout memory management operations, Linux utilizes the page structure as the definitive abstract descriptor to represent physical memory frames. Because distinct runtime execution paths and functional workloads alter the core characteristics of a page along with the explicit tracking variables required to audit it, the structural configuration of the page structure must adapt dynamically. Linux achieves this modular variance by composing the page structure with nested C language union datatypes, which facilitates the reuse of a single descriptor canvas across multiple distinct allocation sub-modules. The page structure is declared within the file git/include/linux/mm_types.h.

Every variation of the page structure preserves the flags field as its leading member. Each individual bit within this field represents an explicit operational bitmask, which logs whether a physical page frame fits into a particular category or resides at a specific management phase. These bitmasks are primarily leveraged to determine if a page represents a compound page component, belongs to a specialized SLAB allocation block, acts as a highly active caching node, maps directly onto a file storage object versus an anonymous swap slice, or features contents that can be safely migrated to different nodes. The comprehensive set of page tracking bitmasks supported by Linux is declared within the header file git/include/linux/page-flags.h.

Manipulating these flags typically requires invoking dedicated processing routines to handle runtime testing, bit setting, and clearing passes. When searching for these specific bit manipulation interfaces using tracking utilities, developers often discover that these functions lack explicit definitions inside the source trees. This architecture exists because these interfaces are generated dynamically via macro expansion routines. For instance, the macro execution PAGEFLAG(Unevictable, unevictable, PF_HEAD) generates the complete family of helper functions used to manipulate the PG_unevictable bit flag. The blueprint definition of the PAGEFLAG macro is as follows:

#define PAGEFLAG(uname, lname, policy)					\
	TESTPAGEFLAG(uname, lname, policy)				\
	SETPAGEFLAG(uname, lname, policy)					\
	CLEARPAGEFLAG(uname, lname, policy)

The matching blueprint definitions for the underlying TESTPAGEFLAG, SETPAGEFLAG, and CLEARPAGEFLAG macros are implemented as:

#define TESTSETFLAG(uname, lname, policy)					\
static __always_inline int TestSetPage##uname(struct page *page)			\
	{ return test_and_set_bit(PG_##lname, &policy(page, 1)->flags); }
#define SETPAGEFLAG(uname, lname, policy)					\
static __always_inline void SetPage##uname(struct page *page)			\
	{ set_bit(PG_##lname, &policy(page, 1)->flags); }

and

#define CLEARPAGEFLAG(uname, lname, policy)				\
static __always_inline void ClearPage##uname(struct page *page)			\
	{ clear_bit(PG_##lname, &policy(page, 1)->flags); }

From this macro expansion architecture, it can be determined that evaluating PAGEFLAG(Unevictable, unevictable, PF_HEAD) dynamically expanding code generates the three inline helper functions TestSetPageUnevictable(), SetPageUnevictable(), and ClearPageUnevictable() to execute atomic testing, bitwise setting, and bitwise clearing passes on the PG_unevictable bit mask, respectively. These generated helper functions resolve programmatically to:

static __always_inline int TestSetPageUnevictable(struct page *page)
{
	return test_and_set_bit(PG_unevictable, &PF_HEAD(page, 1)->flags);
}
static __always_inline void SetPageUnevictable(struct page *page)		
{ 
	set_bit(PG_unevictable, &PF_HEAD(page, 1)->flags);
}
static __always_inline void ClearPageUnevictable(struct page *page)		
{ 
	clear_bit(PG_unevictable, &PF_HEAD(page, 1)->flags);
}
Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com